package com.mxchip.livestar.ui.call

import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import com.mxchip.livestar.base.ContactPOJO
import com.mxchip.livestar.repository.ContactRepository
import com.mxchip.livestar.repository.RoomClientRepository
import com.mxchip.livestar.utils.WordToPinYinUtils
import com.videohigh.graham.Term

class AdminViewModel(application: Application) : AndroidViewModel(application) {
    private val searchWord: MutableLiveData<String> by lazy {
        MutableLiveData<String>(null)
    }

    fun searchWord(s: String?) {
        searchWord.value = s
    }

    private val contactRepository = ContactRepository.getInstance(application.applicationContext)

    private val userIds = RoomClientRepository.termIdsAll

    val micUserIds = RoomClientRepository.micUserIds
    private val avDevices = RoomClientRepository.avDevices


    private val _avUsers: MediatorLiveData<List<AVUser>> by lazy {
        MediatorLiveData<List<AVUser>>().apply {
            value = arrayListOf()
        }
    }

    val avUsers: LiveData<List<AVUser>>
        get() = _avUsers


    private val contacts: LiveData<List<ContactPOJO>> by lazy {
        Log.e("ContactViewModel", "Thread.currentThread()" + Thread.currentThread().name)
        contactRepository.all
    }

    private val avUsersObserver = Observer<Any> {

        val contactMap = contacts.value?.map { it.contactNumber to it }?.toMap()

        val list = userIds.value?.map { term ->

            // val uid = term.dbyUid
            val termId = term.termId
            var micEnabled = false
            var cameraLocalEnabled = false
            var cameraHdmiEnabled = false
            var cameraWifiEnabled = false

            var name: String? = null

            micUserIds.value?.let { list ->
                micEnabled = list.any { it.term == term }
            }

            avDevices.value?.filter {
                it.term == term && (it.deviceName == AVDevice.CAMERA_LOCAL || it.deviceName == AVDevice.CAMERA_LOCAL_SMALL)
            }?.let {
                cameraLocalEnabled = it.isNotEmpty()
            }

            avDevices.value?.filter {
                it.term == term && it.deviceName == AVDevice.CAMERA_HDMI
            }?.let {
                cameraHdmiEnabled = it.isNotEmpty()
            }

            avDevices.value?.filter {
                it.term == term && it.deviceName == AVDevice.CAMERA_WIFI
            }?.let {
                cameraWifiEnabled = it.isNotEmpty()
            }

            contactMap?.let { map ->
                map[termId]?.let {
                    name = it.contactName
                }
            }

            AVUser(term, name, micEnabled, cameraLocalEnabled, cameraHdmiEnabled, cameraWifiEnabled)
        }

        _avUsers.value = if (list.isNullOrEmpty()) {
            arrayListOf()
        } else {
            list
        }

    }

    private val _meetingFilterList: MediatorLiveData<List<AVUser>> by lazy {
        MediatorLiveData<List<AVUser>>().also {
            it.value = arrayListOf()
        }
    }

    val meetingFilterList: LiveData<List<AVUser>>
        get() = _meetingFilterList

    private val meetingFilterListObserver: Observer<Any> = Observer {


        val keyword = searchWord.value

        if (keyword.isNullOrBlank()) {
            _meetingFilterList.value = avUsers.value
            return@Observer
        }

        val list = avUsers.value?.filter { contact ->
//            val pingyin = contact.pys
//
//            if (pingyin != null) {
//                if (pingyin.contains(keyword)) {
//                    return@filter true
//                }
//            }
            val name = contact.name
            if (name != null) {
                if (name.contains(keyword)) {
                    return@filter true
                }
                val pingy = WordToPinYinUtils.getInstance().getSelling(name)
                if (!pingy.isNullOrEmpty()
                        && pingy.contains(keyword)) {
                    return@filter true
                }
            }
            val number = contact.term.termId
            if (number != null) {
                if (number.contains(keyword)) {
                    return@filter true
                }
            }
            return@filter false
        }

        _meetingFilterList.value = list
    }

    init {
        _avUsers.addSource(userIds, avUsersObserver)
        _avUsers.addSource(micUserIds, avUsersObserver)
        _avUsers.addSource(avDevices, avUsersObserver)
        _avUsers.addSource(contacts, avUsersObserver)

        _meetingFilterList.addSource(searchWord, meetingFilterListObserver)
        _meetingFilterList.addSource(avUsers, meetingFilterListObserver)
    }

}

data class AVUser(var term: Term, var name: String?, var micEnabled: Boolean, var cameraLocalEnabled: Boolean, var cameraHdmiEnabled: Boolean, var cameraWifiEnabled: Boolean)